﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HDQApp.Common;

namespace HDQApp
{
    public partial class ProcMgrForm : Form
    {
        public ProcMgrForm()
        {
            InitializeComponent();

            //为KeyDown能应用到所有控件上 注册 KeyDown 事件 
            foreach (Control control in this.Controls)
            {
                control.KeyDown += control_KeyDown;
            }
        }

        void control_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
            else if (e.KeyCode == Keys.Delete)
            {
                btnDeleteProc_Click(sender, e);
            }
        }

        private void ProcMgrForm_Load(object sender, EventArgs e)
        {
            Init();
        }

        /// <summary>
        /// 初始化当前数据库中的存储过程
        /// </summary>
        void Init()
        {
            //查询存储过程名称 及 存储过程定义
            string sel_sql = @"select a.NAME,b.definition as value from sys.all_objects a,sys.sql_modules b where a.is_ms_shipped=0 and a.object_id = b.object_id and a.[type] in ('P') order BY a.create_date,a.[name] desc";
            DataTable data = DbHelperMsSQL.QueryTable(sel_sql);

            lBox.DisplayMember = "name";
            lBox.ValueMember = "value";
            lBox.DataSource = data;
        }

        private void btnUpdateProc_Click(object sender, EventArgs e)
        {
            string drop_sql = "DROP PROC " + lBox.Text;
            string procText = rhTboxProc.Text.Trim();
            try
            {
                //事务 先执行 drop 存储过程的语句，再执行create 存储过程的语句
                int res = DbHelperMsSQL.ExecuteSqlTran(new List<string>()
                {
                    drop_sql,procText
                });
                SetMsg(true, "提示：更新成功！");
                Init();
            }
            catch (Exception ex)
            {
                SetMsg(false, "提示：" + ex.Message);
            }
        }

        private void btnDeleteProc_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(lBox.Text))
            {
                DialogResult dResult = MessageBox.Show("确定要删除吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dResult == System.Windows.Forms.DialogResult.OK)
                {
                    string drop_sql = "DROP PROC " + lBox.Text;
                    try
                    {
                        DbHelperMsSQL.ExecuteSql(drop_sql);
                        SetMsg(true, "提示：删除成功！");
                        Init();
                    }
                    catch (Exception ex)
                    {
                        SetMsg(false, "提示：" + ex.Message);
                    }
                }
            }
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void SetMsg(bool isok, string msg)
        {
            labMsg.Text = msg;
            labMsg.ForeColor = isok ? Color.Green : Color.Red;
        }

        private void lBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            string textProc = lBox.SelectedValue.ToString();
            rhTboxProc.Text = textProc;

            //设置光标位置
            rhTboxProc.SelectionStart = 0;
            //滚动到控件光标处 
            rhTboxProc.ScrollToCaret();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            Init();

            SetMsg(true, "提示：刷新完成！");

            //设置光标位置
            rhTboxProc.SelectionStart = 0;
            //滚动到控件光标处 
            rhTboxProc.ScrollToCaret();
        }
    }
}
